package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Entity.AddEvaluation;
import com.example.starseas_phone.Entity.Apply;
import com.example.starseas_phone.Entity.Evaluation;

import org.json.JSONArray;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MarkActivity extends AppCompatActivity {

    private String TAG = "MarkActivity测试";

    private TextView mark_WorkerName;
    private RadioGroup mark_radiogroup1;
    private RadioGroup mark_radiogroup2;
    private RadioGroup mark_radiogroup3;
    private RadioGroup mark_radiogroup4;
    private RadioGroup mark_radiogroup5;
    private Button mark_btn;
    private String WorkerEvaluationOption1="";
    private String WorkerEvaluationOption2="";
    private String WorkerEvaluationOption3="";
    private String WorkerEvaluationOption4="";
    private String WorkerEvaluationOption5="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark);
        Intent intent = getIntent();
        AddEvaluation addEvaluation = (AddEvaluation) intent.getSerializableExtra("addEvaluation");
        initid();
        mark_WorkerName.setText(addEvaluation.getWorkerName());
        mark_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (WorkerEvaluationOption1.equals("")||WorkerEvaluationOption2.equals("")||WorkerEvaluationOption3.equals("")||WorkerEvaluationOption4.equals("")||WorkerEvaluationOption5.equals("")){
                    Toast.makeText(MarkActivity.this,"有选项没填！请将所有选项填写完整,谢谢!",Toast.LENGTH_SHORT).show();
                }else {
                    tijiao(addEvaluation);
                }
            }
        });
        mark_radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.mark_radiogroup1_A){
                    WorkerEvaluationOption1 = "20";
                }else if (i==R.id.mark_radiogroup1_B){
                    WorkerEvaluationOption1 = "15";
                }else if (i==R.id.mark_radiogroup1_C){
                    WorkerEvaluationOption1 = "10";
                }else if (i==R.id.mark_radiogroup1_D){
                    WorkerEvaluationOption1 = "5";
                }else {
                    WorkerEvaluationOption1 = "";
                }
            }
        });

        mark_radiogroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.mark_radiogroup2_A){
                    WorkerEvaluationOption2 = "20";
                }else if (i==R.id.mark_radiogroup2_B){
                    WorkerEvaluationOption2 = "15";
                }else if (i==R.id.mark_radiogroup2_C){
                    WorkerEvaluationOption2 = "10";
                }else if (i==R.id.mark_radiogroup2_D){
                    WorkerEvaluationOption2 = "5";
                }else {
                    WorkerEvaluationOption2 = "";
                }
            }
        });

        mark_radiogroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.mark_radiogroup3_A){
                    WorkerEvaluationOption3 = "20";
                }else if (i==R.id.mark_radiogroup3_B){
                    WorkerEvaluationOption3 = "15";
                }else if (i==R.id.mark_radiogroup3_C){
                    WorkerEvaluationOption3 = "10";
                }else if (i==R.id.mark_radiogroup3_D){
                    WorkerEvaluationOption3 = "5";
                }else {
                    WorkerEvaluationOption3 = "";
                }
            }
        });

        mark_radiogroup4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.mark_radiogroup4_A){
                    WorkerEvaluationOption4 = "20";
                }else if (i==R.id.mark_radiogroup4_B){
                    WorkerEvaluationOption4 = "15";
                }else if (i==R.id.mark_radiogroup4_C){
                    WorkerEvaluationOption4 = "10";
                }else if (i==R.id.mark_radiogroup4_D){
                    WorkerEvaluationOption4 = "5";
                }else {
                    WorkerEvaluationOption4 = "";
                }
            }
        });

        mark_radiogroup5.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.mark_radiogroup5_A){
                    WorkerEvaluationOption5 = "20";
                }else if (i==R.id.mark_radiogroup5_B){
                    WorkerEvaluationOption5 = "15";
                }else if (i==R.id.mark_radiogroup5_C){
                    WorkerEvaluationOption5 = "10";
                }else if (i==R.id.mark_radiogroup5_D){
                    WorkerEvaluationOption5 = "5";
                }else {
                    WorkerEvaluationOption5 = "";
                }
            }
        });
    }

    private void initid(){
        mark_WorkerName = (TextView) findViewById(R.id.mark_WorkerName);
        mark_radiogroup1 = (RadioGroup) findViewById(R.id.mark_radiogroup1);
        mark_radiogroup2 = (RadioGroup) findViewById(R.id.mark_radiogroup2);
        mark_radiogroup3 = (RadioGroup) findViewById(R.id.mark_radiogroup3);
        mark_radiogroup4 = (RadioGroup) findViewById(R.id.mark_radiogroup4);
        mark_radiogroup5 = (RadioGroup) findViewById(R.id.mark_radiogroup5);

        mark_btn = (Button) findViewById(R.id.mark_btn);
    }

    private void tijiao(AddEvaluation addEvaluation){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("Worker_EvaluationId",1);
                    jsonObject1.put("WorkerId",addEvaluation.getWorkerId());
                    jsonObject1.put("StuId",Sto.students.getStuId());
                    jsonObject1.put("WorkerEvaluationOption1",WorkerEvaluationOption1);
                    jsonObject1.put("WorkerEvaluationOption2",WorkerEvaluationOption2);
                    jsonObject1.put("WorkerEvaluationOption3",WorkerEvaluationOption3);
                    jsonObject1.put("WorkerEvaluationOption4",WorkerEvaluationOption4);
                    jsonObject1.put("WorkerEvaluationOption5",WorkerEvaluationOption5);
                    jsonObject1.put("CourseId",addEvaluation.getCourseId());
                    jsonObject1.put("EvaluationOfTime","2020-03-09 08:00:00");

                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    Log.d(TAG,jsonObject1.toString()+"");
                    RequestBody requestBody = RequestBody.create(mediaType,jsonObject1.toString()+"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/WorkerEvaluation/AddWorkerEvaluation?token="+Sto.students.getToken())
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (msg.equals("添加成功")){
                                Toast.makeText(MarkActivity.this,msg,Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(MarkActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}